//
// Created by wenyaxi on 24-11-4.
//
#include <common.h>

#include "json.hpp"

class Solution {
public:
    int segments[4];
    vector<string> res;
    string str;
    int len;

    vector<string> restoreIpAddresses(string s) {
        str = move(s);
        len = str.length();
        dfs(0, 0);
        return res;
    }

    void dfs(int sid, int idx) {
        if (sid == 4) {
            if (idx == len) {
                stringstream ss;
                for (int i = 0; i < 4; i++) {
                    ss << segments[i];
                    if (i != 3) {
                        ss << ".";
                    }
                }
                res.push_back(ss.str());
            }
            return;
        }
        if (idx > len) return;
        if (str[idx] == '0') {
            segments[sid] = 0;
            dfs(sid + 1, idx + 1);
            return;
        }

        int sum = 0;
        for (int i = idx; i < len; i++) {
            sum = sum * 10 + (str[i] - '0');
            if (sum > 0 && sum <= 0xff) {
                segments[sid] = sum;
                dfs(sid + 1, i + 1);
            } else {
                break;
            }
        }
    }
};

int main(int argc, char *argv[]) {
    /*
     *"25525511135"
     *"0000"
     *"101023"
     */
    Solution solution;
    auto basic_strings = solution.restoreIpAddresses("101023");
    nlohmann::json j;
    j["res"] = basic_strings;
    cout << j.dump(2) << endl;
    return 0;
}
